package com.zhongkai.web.control.config;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.model.config.Department;
import com.zhongkai.model.config.User;
import com.zhongkai.service.config.DepartmentService;
import com.zhongkai.service.config.UserService;
import com.zhongkai.tools.CheckWord;

@Controller
public class DepartmentController {
	private Logger log=Logger.getLogger(this.getClass());
	DepartmentService departmentService;
	UserService userService;

	@Resource
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	/*保存*/
	@RequestMapping(value="/system/config/department_add.do")
	public String save(Department department,HttpServletRequest request,HttpServletResponse response) throws IOException{
		try {
			String departmentName=department.getDepartmentName();
			PrintWriter out=response.getWriter();
			/*部门编号校验*/
			if(department.getDepartmentIdentifier()==null||"".equals(department.getDepartmentIdentifier())){
				out.write(CheckWord.showMsg("部门编号不能为空!", "/system/config/department_add_form.jsp",request));
				return null;
			}
			Pattern pattern = Pattern.compile("^\\d{1,11}$");
			if(!pattern.matcher(department.getDepartmentIdentifier()).matches()){
				out.write(CheckWord.showMsg("非法的编号格式!", "/system/config/department_add_form.jsp",request));
				return null;
			}
			List currentDepartmentList=departmentService.findByHql("from Department where departmentIdentifier=?", new Object[]{department.getDepartmentIdentifier()});
			if(currentDepartmentList!=null && currentDepartmentList.size()>0){
				out.write(CheckWord.showMsg("部门编号已被使用!", "/system/config/department_add_form.jsp",request));
				return null;
			}
			/*部门名称校验*/
			if(departmentName==null || "".equals(departmentName)){
				out.write(CheckWord.showMsg("部门名不能为空!", "/system/config/department_add_form.jsp",request));
				return null;
			}
			
			currentDepartmentList=departmentService.findByHql("from Department where departmentName=?", new Object[]{departmentName});
			if(currentDepartmentList!=null && currentDepartmentList.size()>0){
				out.write(CheckWord.showMsg("部门名已被使用!", "/system/config/department_add_form.jsp",request));
				return null;
			}
			else{
				departmentService.save(department);
				out.write(CheckWord.showMsg("部门保存成功!", "/system/config/department_manage_list.jsp",request));
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("注册时发生异常，请与管理员联系!", "/system/config/department_add_form.jsp",request));
			return null;
		}
	}
	
	/*更新*/
	@RequestMapping(value="/system/config/department_manage_modify.do")
	public String updateDepartment(HttpServletResponse response,HttpServletRequest request,Department department) throws Exception{
		try {
			Integer departmentId=department.getDepartmentId();
			String departmentName=department.getDepartmentName();
			PrintWriter out=response.getWriter();
			/*部门编号校验*/
			if(department.getDepartmentIdentifier()==null||"".equals(department.getDepartmentIdentifier())){
				out.write(CheckWord.showMsg("部门编号不能为空!", "/system/config/department_manage_modify.jsp?departmentId="+departmentId,request));
				return null;
			}
			Pattern pattern = Pattern.compile("^\\d{1,11}$");
			if(!pattern.matcher(department.getDepartmentIdentifier()).matches()){
				out.write(CheckWord.showMsg("编号过长或非法!", "/system/config/department_manage_modify.jsp?departmentId="+departmentId,request));
				return null;
			}
			List currentDepartmentList=departmentService.findByHql("from Department where departmentIdentifier=?", new Object[]{department.getDepartmentIdentifier()});
			if(currentDepartmentList!=null && currentDepartmentList.size()>0){
				out.write(CheckWord.showMsg("部门编号已被使用!", "/system/config/department_manage_modify.jsp?departmentId="+departmentId,request));
				return null;
			}
			if(departmentName==null || departmentName.equals("")){
				out.write(CheckWord.showMsg("部门名不能为空!", "/system/config/department_manage_modify.jsp?departmentId="+departmentId,request));
				return null;
			}
			
			currentDepartmentList=departmentService.findByHql("from Department where departmentName=?and departmentIdentifier=?", new Object[]{departmentName,department.getDepartmentIdentifier()});
			if(currentDepartmentList!=null && currentDepartmentList.size()>0){
				out.write(CheckWord.showMsg("部门名未更新/已存在!", "/system/config/department_manage_modify.jsp?departmentId="+departmentId,request));
				return null;
			}
			else{
				//Department currentDepartment=departmentService.findById(department.getDepartmentId());
				//department.setDepartmentId(currentDepartment.getDepartmentId());
				departmentService.merge(department);
				out.write(CheckWord.showMsg("部门更新成功!", "/system/config/department_manage_list.jsp",request));
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("更新部门时发生异常，请与管理员联系!", "/system/config/department_manage_list.jsp",request));
			return null;
		}
	}
	
	@RequestMapping(value="/system/config/department_manage_delete.do")
	public String deleteDepartment(HttpServletRequest request,HttpServletResponse response,int departmentId) throws IOException {
		try {
			Department currentDepartment=departmentService.findById(departmentId);
			PrintWriter out=response.getWriter();
			if(currentDepartment==null){
				out.write(CheckWord.showMsg("该记录不存在!!", "/system/config/department_manage_list.jsp",request));
				return null;
			}
			List<User> userlist=userService.findByHql("from User where departmentId=?", new Object[]{departmentId});
			if(userlist!=null&&userlist.size()>0){
				out.write(CheckWord.showMsg("删除失败，请先删掉部门下的用户!", "/system/config/department_manage_list.jsp",request));
				return null;
			}
			else{
				departmentService.delete(currentDepartment);
				out.write(CheckWord.showMsg("删除成功!!", "/system/config/department_manage_list.jsp",request));
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			PrintWriter out=response.getWriter();
			out.write(CheckWord.showMsg("删除时出现异常，请与管理员联系!", "/system/config/department_manage_list.jsp",request));
			return null;
		}
	}
	
}
